package com.gxuwz.Market.business.dao;

import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gxuwz.Market.business.bd.DBUtil;
import com.gxuwz.Market.business.entity.ServiceTask;
import com.gxuwz.core.dao.impl.BaseDaoImpl;
import com.gxuwz.core.pagination.Result;

import util.MyTime;



@Repository("serviceTaskDAO")
public class ServiceTaskDAO extends BaseDaoImpl<ServiceTask>{
	/**
	 * 根据条件查找分页
	 * @param ServiceTask 模型
	 * @param page 第几页
	 * @param row 长度
	 * @return
	 * @author 李宪乾
	 * @date 2017.9.27
	 */
	@SuppressWarnings("unchecked")
	public Result<ServiceTask> find(ServiceTask serviceTask, int page, int size){
		String queryString = "from ServiceTask where 1=1";
		if(null != serviceTask.getId() && !"".equals(serviceTask.getId())){			
			queryString = queryString + "and id like '%"+serviceTask.getId()+"%'";
		}
		if(null != serviceTask.getTaskId() && !"".equals(serviceTask.getTaskId())){			
			queryString = queryString + "and collegeId like '%"+serviceTask.getTaskId()+"%'";
		}
		if(null != serviceTask.getTaskName() && !"".equals(serviceTask.getTaskName())){			
			queryString = queryString + "and taskName like '%"+serviceTask.getTaskName()+"%'";
		}
		if(null != serviceTask.getTaskState() && !"".equals(serviceTask.getTaskState())){
			queryString = queryString +" and taskState = "+serviceTask.getTaskState()+" ";
		}
		int start=(page-1)*size;
		int limit =size;
		return (Result<ServiceTask> )super.find(queryString, null, null, start, limit);
	}
	/**
	 * 根据状态查找数据
	 * @param taskState
	 * @return
	 */
	public List<ServiceTask> findByState(int taskState){
		List<ServiceTask> ServiceTaskList = null;
		String queryString = "from ServiceTask where taskState="+taskState;
		ServiceTaskList = this.getHibernateTemplate().find(queryString);
		return ServiceTaskList;
	}
	
	public List<ServiceTask> findAll() {
		List<ServiceTask> ServiceTaskList = null;
		String queryString = "from ServiceTask where 1=1";
		ServiceTaskList = this.getHibernateTemplate().find(queryString);
		return ServiceTaskList;
	}
	/**
	 * 根据id查询单个用户
	 * @param userId
	 * @return
	 */
	public ServiceTask findById(int id){
		List<ServiceTask> ServiceTaskList = null;
		String queryString = "from ServiceTask where id ="+id;
		try{
			ServiceTaskList = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return ServiceTaskList.get(0);
	}
	
	public List<ServiceTask> findByProperty(String property, String value){
		List<ServiceTask> list = null;
		String queryString = "from ServiceTask where "+property+" ='"+value+"' ";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	
	public void checkTime(){
		Connection conn = DBUtil.getConnection();
		StringBuffer sql = new StringBuffer("update service_task set task_state = 1 where task_state = 0 and end_time < '");
		sql.append(MyTime.getNowForTimestamp());
		sql.append("'");
		PreparedStatement ptmt;
		try {
			ptmt = conn.prepareStatement(sql.toString());
			ptmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}